package com.lagou.wab.servlet;

import com.lagou.base.Constants;
import com.lagou.pojo.Course;
import com.lagou.service.CourseService;
import com.lagou.service.impl.CourseServiceImpl;
import com.lagou.utils.DateUtils;
import com.lagou.utils.UUIDUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;


@WebServlet("/SaveCourse")
public class SaveCourseInfo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try{
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);

        List<FileItem> list = upload.parseRequest(req);
        HashMap<Object, Object> map = new HashMap<>();

        Course course = new Course();

            //遍历
            for (FileItem item:list){
                boolean formField = item.isFormField();//true false
                if (formField){
                    //普通表单
                    //属性
                    String filedName = item.getFieldName();
                    //值
                    String value = item.getString("utf-8");
                    map.put(filedName,value);
                }else {
                    //文件上传
                    String name = item.getName();//上传文件的名字
                    String newFileName = UUIDUtils.getUUID()+""+name;

                    //读取文件
                    InputStream in = item.getInputStream();

                    //获取路径
                    String realPath = this.getServletContext().getRealPath("/");
                    //C:\Users\hp\IdeaProjects\lagouEdu\webapps\lagou_war
                    String path = realPath.substring(0,realPath.indexOf("lagouEdu_war"));
                    //C:\Users\hp\IdeaProjects\lagouEdu\webapps

                    //输出
                    FileOutputStream out = new FileOutputStream(path + "/public/" + newFileName);

                    //图片路径地址
                    map.put("course_img_url", Constants.LOCAL_URL + "/public/" + newFileName);
                    in.close();
                    out.close();
                }

                BeanUtils.populate(course,map);
                CourseService courseService = new CourseServiceImpl();
                String dateFormart = DateUtils.getDateFormart();

                if (map.get("id")!=null){

                    //修改更新时间
                    course.setUpdate_time(dateFormart);
                    String result = courseService.saveCourseSalesInfo(course);
                    resp.getWriter().print(result);

                }else {
                    //第一次创建
                    course.setCreate_time(dateFormart);
                    course.setUpdate_time(dateFormart);
                    course.setStatus(1);// 上下架
                    String result = courseService.saveCourseSalesInfo(course);
                    resp.getWriter().print(result);
                }
                //补充条件参数

//                course.setCreate_time(dateFormart);
//                course.setUpdate_time(dateFormart);
//                course.setStatus(1);//1  上架  0  下架

//                BeanUtils.populate(course,map);

                String s = courseService.saveCourseSalesInfo(course);
                resp.getWriter().print(s);
            }
    }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);

    }
}
